package servlet;


import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/ResetPasswordServlet.do")
public class reset extends HttpServlet {
    // SearchUserServlet.do
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int id = Integer.parseInt(request.getParameter("userId"));
        boolean success = is_exit(id);

        if (success) {

                response.sendRedirect("usermanager/success.jsp");
            } else {
                response.sendRedirect("usermanager/errorpage.jsp");
            }


    }


    private boolean is_exit(int userId) {

        JDBCUtil jdbcUtil = new JDBCUtil();

        try {
            String sql = "UPDATE 02_38_yangyang SET password = 123456 WHERE user_id = ?";
            ResultSet resultSet = jdbcUtil.prepareQuery(sql, userId);
            if (!resultSet.next()) {
                return true;
            }
        }
        // 处理数据库异常
        catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            jdbcUtil.closeConnection(); // 关闭数据库连接
        }

        return false;
    }
    private List<Map<String, Object>> performSearch(int userid) {
        List<Map<String, Object>> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();

        try {
            String sql = "UPDATE users SET password = 123456 WHERE user_id = ?";
            ResultSet resultSet = jdbcUtil.prepareQuery(sql, userid);

            while (resultSet.next()) {
                String username=resultSet.getString("username");
                int id = resultSet.getInt("user_id");
                String gender = resultSet.getString("gender");
                String profession = resultSet.getString("profession");
                String favor = resultSet.getString("favor");
                String description = resultSet.getString("description");
                int state = resultSet.getInt("state");
                int role = resultSet.getInt("role");

                // 检查是否存在 null 值，避免空指针异常
                if (gender != null && profession != null && favor != null && description != null) {
                    Map<String, Object> user = Map.of(
                            "id", id,
                            "username", username,
                            "gender", gender,
                            "profession", profession,
                            "favor", favor,
                            "description", description,
                            "state", state,
                            "role", role
                    );


                    // 将用户信息Map添加到列表中
                    userList.add(user);
                }
            }
            // 处理数据库异常
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            jdbcUtil.closeConnection(); // 关闭数据库连接
        }

        return userList;
    }

    private List<Map<String, Object>> allquiry() {
        List<Map<String, Object>> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        try {
            String sql = "SELECT * FROM 02_38_yangyang"; // 假设数据库表名为 users
            ResultSet resultSet = jdbcUtil.query(sql);

            while (resultSet.next()) {
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                String gender = resultSet.getString("gender");
                String profession = resultSet.getString("profession");
                String favor = resultSet.getString("favor");
                String description = resultSet.getString("description");

                // Check for null values before adding to the map
                if (username != null && password != null && gender != null && profession != null && favor != null && description != null) {
                    Map<String, Object> user = Map.of(
                            "username", username,
                            "gender", gender,
                            "profession", profession,
                            "favor", favor,
                            "description", description
                    );

                    userList.add(user);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 处理数据库异常
            // 可以跳转到错误页面或返回错误信息
        } finally {
            jdbcUtil.closeConnection();
        }
        return  userList;
    }
}


